<?php
namespace app\admin\controller;

use app\admin\model\AdminRole;
use app\admin\validate\IDVali;
use app\admin\validate\RoleVali;
use app\lib\exception\ParamException;

class Role extends Base {
    public function index() {
        $data = AdminRole::getAdminRoleList();

        $this->assign( 'list', $data );
        $this->assign('page', $data->render());

        return $this->fetch();
    }

    // 添加
    function add () {
        return $this->fetch();
    }

    // 菜单授权
    // 验证有问题
    function doMenuAuth() {
        // 定义的goCheck方法有问题
//        $req = (new RoleVali())->goCheck('auth');

        $req = $this->request->param();
        $res = $this->validate( $req, array(
            'ids' => 'require',
            'role_id' => 'require'
        ) );
        if( !$res ){
            throw new ParamException([
                'msg' => $this->error
            ]);
        }

        $role = AdminRole::get( array( 'role_id' => $req['role_id'] ) );
        $roleMenuAuth = (array)$role->menu_auth;
        if ( empty($req['ids']) ) {
            $final = '';
        } else {
//            $newMenuAuth = array_unique( array_merge( (array)$role->menu_auth, $req['ids'] ) );
            $diff = array_diff( $req['ids'], $roleMenuAuth );
            if ( $diff ) {
                // 添加
                $newMenuAuth = array_unique( array_merge( $roleMenuAuth, $req['ids'] ) );
            } else {
                // 删除
                $newMenuAuth = array_diff($roleMenuAuth, $req['ids']);
            }

            $temp = implode( ',',  $newMenuAuth);
            $final = explode(',', $temp);
        }

        $data = array(
            'menu_auth' => $final
        );

        $adminRole = new AdminRole();
        $adminRole->allowField(true)->save( $data, array('role_id' => $req['role_id']) );

        return json( array( 'error_code' => 1, 'msg' => '操作成功', 'data' =>  $req, 'debug' => $final ) );
    }

    function doAdd() {
        $req = (new RoleVali())->goCheck('add');
        if ( !isset($req['ids']) ) {
            throw new ParamException(array( 'msg' => '菜单权限不能为空' ));
        }

        $menuAuth = explode( ',', implode(',', $req['ids']) );

        $adminRole = new AdminRole();
        $adminRole->allowField(true)->save( $req );

        $adminRole->isUpdate(true)->save( array('menu_auth' => $menuAuth ) );
        return json( array( 'error_code' => 1, 'msg' => '操作成功', 'data' => $req ) );
    }

    // 修改
    function edit () {
        $req = (new IDVali())->goCheck();

        $data = AdminRole::getRoleByID( $req['id'] );

        $this->assign('data', $data);
        return $this->fetch();
    }

    function doEdit() {
        $req = (new RoleVali())->goCheck('edit');

//        Log::write( $req,'print');

        $adminRole = new AdminRole();
        $adminRole->allowField(true)->save( $req, array('role_id' => $req['id']) );

        return json( array( 'error_code' => 1, 'msg' => '操作成功', 'data' => $req ) );
    }

    // 删除
    function del() {
        $req = (new IDVali())->goCheck('basic');

        AdminRole::delRoleByID( $req['id'] );

        return json( array( 'error_code' => 1, 'msg' => '操作成功', 'data' => $req ) );
    }



}
